<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="付款申请单id" prop="fksqId">
        <el-input
          v-model="queryParams.fksqId"
          placeholder="请输入付款申请单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款单编号" prop="bxCode">
        <el-input
          v-model="queryParams.bxCode"
          placeholder="请输入付款单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请日期" prop="bxDate">
        <el-date-picker clearable
          v-model="queryParams.bxDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款类型" prop="bxFyXm">
        <el-input
          v-model="queryParams.bxFyXm"
          placeholder="请输入付款类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请付款金额" prop="bxFyJe">
        <el-input
          v-model="queryParams.bxFyJe"
          placeholder="请输入申请付款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售抵扣金额" prop="deductionAmount">
        <el-input
          v-model="queryParams.deductionAmount"
          placeholder="请输入销售抵扣金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="bxRy">
        <el-input
          v-model="queryParams.bxRy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请部门" prop="bxBm">
        <el-input
          v-model="queryParams.bxBm"
          placeholder="请输入申请部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="bxCusCode">
        <el-input
          v-model="queryParams.bxCusCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="bxCus">
        <el-input
          v-model="queryParams.bxCus"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款单编号" prop="refundCode">
        <el-input
          v-model="queryParams.refundCode"
          placeholder="请输入退款单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外协加工编码" prop="outsourcingCode">
        <el-input
          v-model="queryParams.outsourcingCode"
          placeholder="请输入外协加工编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商编码" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入供应商编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="bxCgGys">
        <el-input
          v-model="queryParams.bxCgGys"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商分类" prop="supplierCategory">
        <el-input
          v-model="queryParams.supplierCategory"
          placeholder="请输入供应商分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="未付金额" prop="unpaidAmount">
        <el-input
          v-model="queryParams.unpaidAmount"
          placeholder="请输入未付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已付金额" prop="paidAmount">
        <el-input
          v-model="queryParams.paidAmount"
          placeholder="请输入已付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算期限" prop="settlementPeriod">
        <el-input
          v-model="queryParams.settlementPeriod"
          placeholder="请输入结算期限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="逾期金额" prop="overdueAmount">
        <el-input
          v-model="queryParams.overdueAmount"
          placeholder="请输入逾期金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceTitle">
        <el-input
          v-model="queryParams.invoiceTitle"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票税号" prop="invoiceTaxNumber">
        <el-input
          v-model="queryParams.invoiceTaxNumber"
          placeholder="请输入发票税号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户银行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款状态" prop="bxCnStatus">
        <el-select v-model="queryParams.bxCnStatus" placeholder="请选择付款状态" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="付款人" prop="bxFkr">
        <el-input
          v-model="queryParams.bxFkr"
          placeholder="请输入付款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款时间" prop="bxPayDate">
        <el-date-picker clearable
          v-model="queryParams.bxPayDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择付款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款凭证" prop="bxFkPic">
        <el-input
          v-model="queryParams.bxFkPic"
          placeholder="请输入付款凭证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否需要同步" prop="bxSftb">
        <el-input
          v-model="queryParams.bxSftb"
          placeholder="请输入是否需要同步"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fina:pay:application:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fina:pay:application:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fina:pay:application:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-refresh"
            size="mini"
            @click="handleSync"
            v-hasPermi="['fina:pay:application:sync']"
        >数据同步</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fina:pay:application:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="serialNum" />
      <el-table-column label="付款申请单id" align="center" prop="fksqId" />
      <el-table-column label="付款单编号" align="center" prop="bxCode" />
      <el-table-column label="申请日期" align="center" prop="bxDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bxDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款类型" align="center" prop="bxFyXm" />
      <el-table-column label="付款原因" align="center" prop="bxFkyy" />
      <el-table-column label="申请付款金额" align="center" prop="bxFyJe" />
      <el-table-column label="销售抵扣金额" align="center" prop="deductionAmount" />
      <el-table-column label="备注" align="center" prop="bxFyRemark" />
      <el-table-column label="申请人" align="center" prop="bxRy" />
      <el-table-column label="申请部门" align="center" prop="bxBm" />
      <el-table-column label="客户编码" align="center" prop="bxCusCode" />
      <el-table-column label="客户名称" align="center" prop="bxCus" />
      <el-table-column label="退款单编号" align="center" prop="refundCode" />
      <el-table-column label="外协加工编码" align="center" prop="outsourcingCode" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column label="供应商名称" align="center" prop="bxCgGys" />
      <el-table-column label="供应商分类" align="center" prop="supplierCategory" />
      <el-table-column label="未付金额" align="center" prop="unpaidAmount" />
      <el-table-column label="已付金额" align="center" prop="paidAmount" />
      <el-table-column label="结算期限" align="center" prop="settlementPeriod" />
      <el-table-column label="逾期金额" align="center" prop="overdueAmount" />
      <el-table-column label="发票抬头" align="center" prop="invoiceTitle" />
      <el-table-column label="发票税号" align="center" prop="invoiceTaxNumber" />
      <el-table-column label="开户银行" align="center" prop="bankName" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="付款状态" align="center" prop="bxCnStatus" />
      <el-table-column label="付款人" align="center" prop="bxFkr" />
      <el-table-column label="付款时间" align="center" prop="bxPayDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bxPayDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款凭证" align="center" prop="bxFkPic" />
      <el-table-column label="是否需要同步" align="center" prop="bxSftb" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fina:pay:application:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fina:pay:application:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改付款申请单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="付款申请单id" prop="fksqId">
          <el-input v-model="form.fksqId" placeholder="请输入付款申请单id" />
        </el-form-item>
        <el-form-item label="付款单编号" prop="bxCode">
          <el-input v-model="form.bxCode" placeholder="请输入付款单编号" />
        </el-form-item>
        <el-form-item label="申请日期" prop="bxDate">
          <el-date-picker clearable
            v-model="form.bxDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款类型" prop="bxFyXm">
          <el-input v-model="form.bxFyXm" placeholder="请输入付款类型" />
        </el-form-item>
        <el-form-item label="付款原因" prop="bxFkyy">
          <el-input v-model="form.bxFkyy" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请付款金额" prop="bxFyJe">
          <el-input v-model="form.bxFyJe" placeholder="请输入申请付款金额" />
        </el-form-item>
        <el-form-item label="销售抵扣金额" prop="deductionAmount">
          <el-input v-model="form.deductionAmount" placeholder="请输入销售抵扣金额" />
        </el-form-item>
        <el-form-item label="备注" prop="bxFyRemark">
          <el-input v-model="form.bxFyRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请人" prop="bxRy">
          <el-input v-model="form.bxRy" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请部门" prop="bxBm">
          <el-input v-model="form.bxBm" placeholder="请输入申请部门" />
        </el-form-item>
        <el-form-item label="客户编码" prop="bxCusCode">
          <el-input v-model="form.bxCusCode" placeholder="请输入客户编码" />
        </el-form-item>
        <el-form-item label="客户名称" prop="bxCus">
          <el-input v-model="form.bxCus" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="退款单编号" prop="refundCode">
          <el-input v-model="form.refundCode" placeholder="请输入退款单编号" />
        </el-form-item>
        <el-form-item label="外协加工编码" prop="outsourcingCode">
          <el-input v-model="form.outsourcingCode" placeholder="请输入外协加工编码" />
        </el-form-item>
        <el-form-item label="供应商编码" prop="supplierCode">
          <el-input v-model="form.supplierCode" placeholder="请输入供应商编码" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="bxCgGys">
          <el-input v-model="form.bxCgGys" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="供应商分类" prop="supplierCategory">
          <el-input v-model="form.supplierCategory" placeholder="请输入供应商分类" />
        </el-form-item>
        <el-form-item label="未付金额" prop="unpaidAmount">
          <el-input v-model="form.unpaidAmount" placeholder="请输入未付金额" />
        </el-form-item>
        <el-form-item label="已付金额" prop="paidAmount">
          <el-input v-model="form.paidAmount" placeholder="请输入已付金额" />
        </el-form-item>
        <el-form-item label="结算期限" prop="settlementPeriod">
          <el-input v-model="form.settlementPeriod" placeholder="请输入结算期限" />
        </el-form-item>
        <el-form-item label="逾期金额" prop="overdueAmount">
          <el-input v-model="form.overdueAmount" placeholder="请输入逾期金额" />
        </el-form-item>
        <el-form-item label="发票抬头" prop="invoiceTitle">
          <el-input v-model="form.invoiceTitle" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="发票税号" prop="invoiceTaxNumber">
          <el-input v-model="form.invoiceTaxNumber" placeholder="请输入发票税号" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="付款状态">
          <el-radio-group v-model="form.bxCnStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="付款人" prop="bxFkr">
          <el-input v-model="form.bxFkr" placeholder="请输入付款人" />
        </el-form-item>
        <el-form-item label="付款时间" prop="bxPayDate">
          <el-date-picker clearable
            v-model="form.bxPayDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款凭证" prop="bxFkPic">
          <el-input v-model="form.bxFkPic" placeholder="请输入付款凭证" />
        </el-form-item>
        <el-form-item label="是否需要同步" prop="bxSftb">
          <el-input v-model="form.bxSftb" placeholder="请输入是否需要同步" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplication, getApplication, delApplication, addApplication, updateApplication ,payApplicationSync} from "@/api/fina/payApplication";

export default {
  name: "Application",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款申请单表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fksqId: null,        bxCode: null,        bxDate: null,        bxFyXm: null,        bxFkyy: null,        bxFyJe: null,        deductionAmount: null,        bxFyRemark: null,        bxRy: null,        bxBm: null,        bxCusCode: null,        bxCus: null,        refundCode: null,        outsourcingCode: null,        supplierCode: null,        bxCgGys: null,        supplierCategory: null,        unpaidAmount: null,        paidAmount: null,        settlementPeriod: null,        overdueAmount: null,        invoiceTitle: null,        invoiceTaxNumber: null,        bankName: null,        bankAccount: null,        bxCnStatus: null,        bxFkr: null,        bxPayDate: null,        bxFkPic: null,        bxSftb: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询付款申请单列表 */
    getList() {
      this.loading = true;
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNum: null,        fksqId: null,        bxCode: null,        bxDate: null,        bxFyXm: null,        bxFkyy: null,        bxFyJe: null,        deductionAmount: null,        bxFyRemark: null,        bxRy: null,        bxBm: null,        bxCusCode: null,        bxCus: null,        refundCode: null,        outsourcingCode: null,        supplierCode: null,        bxCgGys: null,        supplierCategory: null,        unpaidAmount: null,        paidAmount: null,        settlementPeriod: null,        overdueAmount: null,        invoiceTitle: null,        invoiceTaxNumber: null,        bankName: null,        bankAccount: null,        bxCnStatus: "0",        bxFkr: null,        bxPayDate: null,        bxFkPic: null,        bxSftb: null,        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加付款申请单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNum = row.serialNum || this.ids
      getApplication(serialNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改付款申请单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNum != null) {
            updateApplication(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplication(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSync() {
      if (this.loading) {
        this.$modal.message("正常进行中，请不要重复点击...");
        return;
      }
      this.loading = true;
      this.$modal.confirm('是否确认同步数据？').then(() => {
        payApplicationSync().then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess("同步成功");
            this.getList();
          } else {
            this.$modal.msgError("同步失败");
          }
        }).catch(() => {
          this.$modal.msgError("同步失败");
        }).finally(() => {
          this.loading = false;
        });
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNums = row.serialNum || this.ids;
      this.$modal.confirm('是否确认删除付款申请单编号为"' + serialNums + '"的数据项？').then(function() {
        return delApplication(serialNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fina/pay/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
